#pragma once

#include <iostream>
#include <vector>
#include <unistd.h>
#include <fcntl.h>

namespace ns_util{
    void SetNonBlock(int sock)
    {
        int fl = fcntl(sock, F_GETFL);
        fcntl(sock, F_SETFL, fl | O_NONBLOCK);
    }

    class StringUtil{
        public:
            static void Split(std::string &in, std::vector<std::string> *out, std::string sep)
            {
                //aaaaXbbbbXccccX ->X：代表的是一个完整报文结束
                //aaaaXbbbbXccc
                //aa
                //""
                while(true){
                    size_t pos = in.find(sep);
                    if(pos == std::string::npos){
                        break;
                    }

                    std::string s = in.substr(0, pos);
                    out->push_back(s);
                    in.erase(0, pos+sep.size());
                }

            }
            static void Deserialize(std::string &in, int *x, int *y)
            {
                //1+4,100+400
                size_t pos = in.find("+");
                std::string left = in.substr(0, pos);
                std::string right = in.substr(pos+1);

                *x = atoi(left.c_str());
                *y = atoi(right.c_str());
            }
    };
}
